package org.lex.io;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class CountingOutputStream2 extends FilterOutputStream {
	private volatile long writeBytesCount = 0L;

	public CountingOutputStream2(OutputStream out) {
		super(out);
	}

	public long getWriteBytesCount() {
		return this.writeBytesCount;
	}

	public void resetCounter() {
		this.writeBytesCount = 0L;
	}

	@Override
	public void close() throws IOException {
		super.close();
	}

	@Override
	public void flush() throws IOException {
		super.flush();
	}

	@Override
	public void write(byte[] b) throws IOException {
		this.write(b, 0, b.length);
	}

	@Override
	public void write(byte[] b, int off, int len) throws IOException {
		if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
			throw new IndexOutOfBoundsException();
		for (int i = 0; i < len; i++) {
			this.writeOneByte(b[off + i]);
		}
	}

	@Override
	public void write(int b) throws IOException {
		this.writeOneByte(b);
	}

	private void writeOneByte(int b) throws IOException {
		super.write(b);
		this.writeBytesCount++;
	}
}
